package com.jerry.lastgomall.product.web;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {
    @Autowired
    private RedissonClient redissonClient;



    @ResponseBody
    @GetMapping("/hello")
    public String testRedisson(){
        //1.获取一把锁,只要锁名一样,就是同一把锁
        RLock myLock = redissonClient.getLock("myLock");

        //2.加锁 手动加锁
        myLock.lock(10, TimeUnit.SECONDS);  //阻塞式等待
        try{
            System.out.println("加锁成功,执行业务:"+Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e) {

        } finally {
            //3.调用解锁
            System.out.println("释放锁:"+Thread.currentThread().getId());
            myLock.unlock();
        }
        return "hello";
    }



}
